package com.jingtai.common.utils.random;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;


public class TokenUtil {
    public final static SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMddHHmmss");
    public final static String PREX = "LHT_";


    public static String generateUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    public static String generateOrderNo() {
        Snowflake snowflake = IdUtil.getSnowflake(0, 0);
        return String.valueOf(snowflake.nextId());
    }

    /**
     * 生成 RosterId
     * */
    public synchronized static String generateRosterId(){
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        return String.valueOf(snowflake.nextId());
    }
    /**
     * 生成 ReceiveId
     * */
    public synchronized static String generateReceiveId(){
        Snowflake snowflake = IdUtil.getSnowflake(2, 2);
        return String.valueOf(snowflake.nextId());
    }
    /**
     * 生成 DetailedId
     * */
    public synchronized static String generateDetailedId(){
        Snowflake snowflake = IdUtil.getSnowflake(3, 3);
        return String.valueOf(snowflake.nextId());
    }

    /**
     * 根据openId生成token
     * */
    public static String generateToken(String openId) {
        if (StringUtils.isEmpty(openId)) {
            return "";
        }
        Date date = Calendar.getInstance().getTime();
        StringBuilder sb = new StringBuilder();
        sb.append(PREX).append(SDF.format(date)).append(openId);
        String token = AESUtil.encrypt(sb.toString());
        return token;
    }

    /**
     * 根据token生成openId
     * */
    public static String getOpenId(String token) {
        if (StringUtils.isEmpty(token)) {
            return "";
        }

        String decrypt = AESUtil.decrypt(token);
        if (!decrypt.startsWith(PREX) || decrypt.length() <= 17) {
            return "";
        }
        String openId = decrypt.substring(18);
        return openId;
    }

    /**
     * 生成长度为length的随机数字
     * */
    public static String generateCode(int length) {
        StringBuffer str = new StringBuffer();
        int  maxNum = 9;
        int count = 0;
        char[] charArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        Random random = new Random();
        while (count < length) {
            int i = Math.abs(random.nextInt(maxNum));
            if (i >= 0 && i < charArr.length) {
                str.append(charArr[i]);
                count ++;
            }
        }

        return str.toString();
    }

    /**
     * 生成长度为length的随机数字
     * */
    public static String generateUppercaseLetter(int length) {
        StringBuffer str = new StringBuffer();
        int  maxNum = 26;
        int count = 0;
        char[] charArr = {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C',
                'V','B','N','M'};
        Random random = new Random();
        while (count < length) {
            int i = Math.abs(random.nextInt(maxNum));
            if (i >= 0 && i < charArr.length) {
                str.append(charArr[i]);
                count ++;
            }
        }

        return str.toString();
    }


//    public static void main(String[] args) {
//        String a = "【仁仁科技】您本次的操作的验证码是:9527,3分钟有效。如非本人操作，请忽略本条短信";
//        String[] code = a.split(":");
//        String b = code[1].substring(0,4);
//        System.out.println(b);
//    }
}
